package com.mdc.controller;

import com.mdc.model.User;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author Mordecai
 * @version 1.0.0
 * @blog https://gitee.com/mordeky/SpringBoot
 *
 */
@RestController
@RequestMapping("/request") // 根路径
public class HelloRequestMapping {
    @RequestMapping("") // 不管是否定义了根路径，("")都等同于("/")
    //@RequestMapping("/") // 如果定义了根路径，则("")不等于("/")
    //@GetMapping("/")
    //@RequestMapping(value="/")
    //@RequestMapping(method=RequestMethod.GET, value="/")
    //@RequestMapping(value="/", method = RequestMethod.GET)
    public String getX(){ return "Hello, World!"; }

    // http://localhost:8080/request/?str1=hello&k=2
    @PostMapping("/")
    //@RequestMapping(value="/", method=RequestMethod.POST)
    public String postX(@RequestParam String str1, @RequestParam int k){
        /*参数列表中的@RequestParam可以省略*/
        return "Post str1='" + str1 + "', k=" + k + "!";
    }

    // http://localhost:8080/request/user/?id=15&name=Rose&age=18
    @PostMapping("user") // 等同于("/user")
    public String postX(@ModelAttribute User usr) {
        /*参数中的 @ModelAttribute 可以省略*/
        return "Post User=(" + usr.getName() + "," + usr.getAge() + ").";
    }

    // http://localhost:8080/request/115
    @GetMapping("/{id}")
    // @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public String getX(@PathVariable Long id) {
        // URL中的id可通过@PathVariable绑定到函数的参数中
        return "Get " + id + "!";
    }

    // http://localhost:8080/request/115?str=Hello!
    @PutMapping("/{id}")
    // @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String putX(@PathVariable Long id, String str) {
        return "Put '" + str + "' to '" + id + "'.";
    }

    //http://localhost:8080/request/115
    @DeleteMapping("/{id2}")
    // @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteX(@PathVariable Long id2) {
        return "Delete id = '" + id2 + "'.";
    }
}